// javamaps
// Copyright (c) 2010 Lup Gratian
package Core;
import java.io.*;

public class ObjectId implements Serializable {
    private static int lastId_;
    private int id_;
    
    /*
     * Constructori.
     */
    static {
        // ID-urile permise sunt intre 0 si Integer.MAX_VALUE.
        lastId_ = 0;
    }

    public ObjectId(int id) {
        id_ = id;
    }

    public ObjectId(ObjectId other) {
        id_ = other.id_;
    }

    /*
     * Metode publice.
     */
    // Genereaza un nou ID.
    public static synchronized ObjectId NewId() {
        return new ObjectId(lastId_++);
    }

    public static synchronized void ResetId() {
        lastId_ = 0;
    }

    public static synchronized void SetStartId(int id) {
        assert(id >= 0);
        assert(id < Integer.MAX_VALUE);
        // ------------------------------------------------
        lastId_ = id;
    }

    // Nu este recomandata folosirea. Pentru a testa daca doua
    // ID-uri sunt egale se foloseste a.equals(b).
    public int Id() { return id_; }

    @Override
    public boolean equals(Object obj) {
        if(obj == null) return false;
        if(this == obj) return true;
        if(this.getClass() != obj.getClass()) return false;

        return id_ == ((ObjectId)obj).id_;
    }

    @Override
    public int hashCode() {
        return id_;
    }

    // Pentru debugging.
    @Override
    public String toString() {
        return "ID=" + id_;
    }
}